package com.qire.manhua.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.just.agentweb.AgentWeb;
import com.qire.antscore.annotation.RouteMapping;
import com.qire.antsrouter.inject.Autowired;
import com.qire.common.constant.CustomEvent;
import com.qire.common.constant.Url;
import com.qire.common.support.base.BaseActivity;
import com.qire.common.support.base.ProxyViewModel;
import com.qire.common.constant.Navigation;
import com.qire.common.inject.ViewModelInjector;
import com.qire.common.basic.StringUtil;
import com.qire.common.utils.NoticeEventBus;
import com.qire.common.utils.NoticeEventBus.NoticeEvent;
import com.qire.common.utils.OkGoConfigHelper;
import com.qire.common.utils.ThemeUtil;
import com.qire.common.utils.ToastHelper;
import com.qire.manhua.R;
import com.qire.manhua.databinding.ActivityWebBinding;
import com.qire.manhua.model.viewModel.IWebViewModel;

@RouteMapping(path = Navigation.Page.WebPage)
public class WebActivity extends BaseActivity<ActivityWebBinding> {

    @Autowired(injector = ViewModelInjector.class)
    ProxyViewModel<IWebViewModel> viewModel;
    @Autowired()
    String title;
    @Autowired()
    String url;

    private AgentWeb agentWeb;

    @Override
    public int layoutRid() {
        return R.layout.activity_web;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ThemeUtil.changeStatusBarLightOrDark(this, true, true);

        if(StringUtil.isEmpty(url)){
            ToastHelper.showToast("未指定跳转URL");
            finish();
            return;
        }

        viewModel.set("title", title);
        viewModel.set("navigationVisible", StringUtil.isEmpty(title) ? View.GONE : View.VISIBLE);

        agentWeb = AgentWeb.with(this)
                .setAgentWebParent((LinearLayout) viewDataBinder().getRoot(), new LinearLayout.LayoutParams(-1, -1))
                .useDefaultIndicator()
                .additionalHttpHeader(Url.domain_m, OkGoConfigHelper.headerMap)
                .createAgentWeb()
                .ready()
                .go(url);

//        agentWeb.getJsInterfaceHolder().addJavaObject()
//
//        agentWeb.getWebCreator().getWebView().setWebViewClient(new WebViewClient(){
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                ToastHelper.showToast(url);
//                return false;
//            }
//        });
    }

    @Override
    protected void registerObserver() {
        super.registerObserver();
        NoticeEventBus.NOTICE.subscribeEvent(CustomEvent.WEB_JAVASCRIPT_CALLBACK, this::onJavascriptCallback);
    }

    @Override
    protected void unregisterObserver() {
        super.unregisterObserver();
        NoticeEventBus.NOTICE.unSubscribeEvent(CustomEvent.WEB_JAVASCRIPT_CALLBACK);
    }

    private boolean onJavascriptCallback(NoticeEvent<String> event) {
        String callback = event.getEventData();
        switch (callback) {
            case "finish@WebActivity":
                finish();
                break;
            default:
                executeCallback(callback);
        }
        return true;
    }

    /**
     * 执行 javascript 回调
     * @param javascript H5里的 javascript 代码。
     */
    private void executeCallback(String javascript) {
        if(agentWeb != null) {
            agentWeb.getJsAccessEntrace().quickCallJs(javascript);
        }
    }

    public void closeBtnOnClick(View view) {
        finish();
    }

}
